// pages/corporate/talent/index.js
import {getUserList} from '../../../api/jobs';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectedEducation: "学历",
    educationList: ["不限", "小学","初中","高中", "中专", "大专", "本科", "硕士", "博士"],
    isBottomSheetVisible: false,
    sheetType: "",
    genderList: ["18岁-25岁", "36岁-40岁", "51岁-55岁","26岁-30岁", "41岁-45岁", "56岁及以上","31岁-35岁", "46岁-50岁", "全部年龄"],
    selectedGender: "年龄",
    work_list : [
      '分拣员','快递员','跑腿代办','送货司机','安保','建筑/房地产','驾驶员','装卸/搬运','驾驶员','操作工','物流','司机','交通/物流/仓储'
    ],
    work_type_list : [
      '物流配送','营销宣传','行政','服务业','市场营销','餐饮行业','影视传媒'
    ],
    selectedWorkType:"",
    selectedWork:"全部",
    jobTypeIcon: "cuIcon-unfold",
    jobLocationIcon: "cuIcon-unfold",
    educationIcon: "cuIcon-unfold",
    genderIcon: "cuIcon-unfold",
    workTypeIcon: "cuIcon-unfold",
    userList: [
     
    ],
    pageNo : 1,
    pageSize : 10,
    loading: false,
    noMoreData: false,
    isModalVisible : false,
  },


  contact(){
    console.log(1)
    this.setData({
      isModalVisible : true
    })
   },
     //关闭弹窗
     closePopup() {
      this.setData({
        isBottomSheetVisible: false,
      });
    },
    nav_to(e){
      const url = e.currentTarget.dataset.url
      wx.reLaunch({
        url: url,
      })
    },
    // 选择弹出层选项
    selectSheetOption(e) {
      const value = e.currentTarget.dataset.value;
      if (this.data.sheetType === "education") {
        this.setData({ selectedEducation: value });
      } else if (this.data.sheetType === "gender") {
        this.setData({ selectedGender: value });
      }else if (this.data.sheetType === "workType") {
        this.setData({ selectedWork: value });
      }
    },


        // 确认弹出层
  confirmBottomSheet() {
    console.log(`确认选择：${this.data.selectedEducation || this.data.selectedGender}`);
    this.closeBottomSheet();
  },

    // 关闭弹出层
    closeBottomSheet() {
      const iconKey = `${this.data.sheetType}Icon`;
      this.setData({
        isBottomSheetVisible: false,
        [iconKey]: "cuIcon-unfold",
      });
    },

       //选择岗位类型
       select_work_type(e){
        const value = e.currentTarget.dataset.value;
        this.setData({ selectedWorkType: value });
      },
  
      //选择职位
      select_work(e){
        const value = e.currentTarget.dataset.value;
        this.setData({ selectedWork: value });
      },
   
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
    // 显示底部弹出层
    showBottomSheet(e) {
      const type = e.currentTarget.dataset.type;
      const iconKey = `${type}Icon`;
      this.setData({
        sheetType: type,
        isBottomSheetVisible: true,
        [iconKey]: "cuIcon-fold",
      });
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(3)
    this.loadMoreData()
   
  },

  onReachBottom() {
    this.loadMoreData()
  },

  // 异步加载数据
  async loadMoreData() {
    console.log('异步加载数据')
    // 防止重复加载和无更多数据时继续加载
    if (this.data.loading || this.data.noMoreData) return;

    // 设置加载状态
    this.setData({ loading: true });

    try {
      // 获取新数据
      const newItems = await this.getListData();
      if (newItems.length === 0) {
        // 如果没有数据，标记无更多数据
        this.setData({ noMoreData: true });
      } else {
        // 将新数据追加到现有列表中
        this.setData({
          userList: [...this.data.userList, ...newItems],
          pageNo: this.data.pageNo + 1, // 页码递增
        });
      }
    } catch (error) {
      console.error('加载更多数据失败:', error);
    } finally {
      // 关闭加载状态
      this.setData({ loading: false });
    }
  },

  //获取数据
  async getListData() {
    try {
      // 调用接口获取数据
      const response = await getUserList({
        pageSize: this.data.pageSize,   
        pageNo: this.data.pageNo, 
      });
      console.log('response.data.list')
      if (response && response.data && response.data.list) {
        console.log(response.data.list)
        return response.data.list; // 返回数据列表
      } else {
        throw new Error('数据格式错误');
      }
    } catch (error) {
      return []; // 返回空数组，避免后续逻辑错误
    }
  },




  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})